<template>
    <el-dialog
        :title="!dataForm.id ? '新增' : '修改'"
        :close-on-click-modal="false"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="100px">
            <el-form-item label="品牌名" prop="name">
                <el-input v-model="dataForm.name" placeholder="品牌名"></el-input>
            </el-form-item>
            <el-form-item label="logo" prop="logo">
<!--                <el-input v-model="dataForm.logo" placeholder="logo"></el-input>-->
                <!-- 引入logo上传控件 -->
                <SingleUpload v-model="dataForm.logo"></SingleUpload>
            </el-form-item>
            <el-form-item label="说明" prop="description">
                <el-input v-model="dataForm.description" placeholder="说明"></el-input>
            </el-form-item>
            <el-form-item label="显示" prop="isshow">
                <el-input v-model="dataForm.isshow" placeholder="显示"></el-input>
            </el-form-item>
            <el-form-item label="检索首字母" prop="firstLetter">
                <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
//引入文件上传控件

import SingleUpload from '../../../components/upload/singleUpload.vue'

export default {
    components: {SingleUpload},
    //输出控件
    comments: {
        SingleUpload
    },
    data () {
        return {
            visible: false,
            dataForm: {
                id: 0,
                name: '',
                logo: '',
                description: '',
                isshow: '',
                firstLetter: '',
                sort: ''
            },
            dataRule: {
                name: [
                    {required: true, message: '品牌名不能为空', trigger: 'blur'}
                ],
                logo: [
                    {required: true, message: 'logo不能为空', trigger: 'blur'}
                ],
                description: [
                    {required: true, message: '说明不能为空', trigger: 'blur'}
                ],
                isshow: [
                    {required: true, message: '显示不能为空', trigger: 'blur'}
                ],
                firstLetter: [ //检索只包含一个大小写字母
                    // {required: true, message: '检索首字母不能为空', trigger: 'blur'}
                    {
                        trigger: 'blur', //触发方式 光标离开
                        //增加自定校验器
                        validator:(rule,value,callback)=>{
                            if(value==''){
                                callback(new Error('检索首字母必须填写'))
                            }else if(!/^[a-zA-Z]$/.test(value)){
                                callback(new Error('检索首字母必须为a-zA-Z'))
                            }else{
                                callback()
                            }
                        }
                    }
                ],
                sort: [
                    // {required: true, message: '排序不能为空', trigger: 'blur'}
                    //排序0-9
                    {
                        trigger: 'blur', //触发方式 光标离开
                        //增加自定校验器
                        validator:(rule,value,callback)=>{
                            if(value==''){
                                callback(new Error('排序不能为空'))
                            }else if(!/^[0-9]$/.test(value)){
                                callback(new Error('排序需为0-9'))
                            }else{
                                callback()
                            }
                        }
                    }
                ]
            }
        }
    },
    methods: {
        init (id) {
            this.dataForm.id = id || 0
            this.visible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    this.$http({
                        // url: `http://localhost:9090/commodity/brand/info/${this.dataForm.id}`,
                        url: this.$http.adornUrl(`/commodity/brand/info/${this.dataForm.id}`),
                        method: 'get',
                        params: this.$http.adornParams()
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.dataForm.name = data.brand.name
                            this.dataForm.logo = data.brand.logo
                            this.dataForm.description = data.brand.description
                            this.dataForm.isshow = data.brand.isshow
                            this.dataForm.firstLetter = data.brand.firstLetter
                            this.dataForm.sort = data.brand.sort
                        }
                    })
                }
            })
        },
        // 表单提交
        dataFormSubmit () {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.$http({
                        // url: `http://localhost:9090/commodity/brand/${!this.dataForm.id ? 'save' : 'update'}`,
                        url: this.$http.adornUrl(`/commodity/brand/${!this.dataForm.id ? 'save' : 'update'}`),
                        method: 'post',
                        data: this.$http.adornData({
                            'id': this.dataForm.id || undefined,
                            'name': this.dataForm.name,
                            'logo': this.dataForm.logo,
                            'description': this.dataForm.description,
                            'isshow': this.dataForm.isshow,
                            'firstLetter': this.dataForm.firstLetter,
                            'sort': this.dataForm.sort
                        })
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
